package com.clevertech.tests.Utils;

import com.thoughtworks.selenium.HttpCommandProcessor;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.unitedinternet.portal.selenium.utils.logging.*;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: pavel
 * Date: Jun 17, 2010
 * Time: 8:56:45 PM
 * To change this template use File | Settings | File Templates.
 */
public class ClevertechSeleniumUtils extends SeleneseTestCase {

     public static LoggingSelenium selenium;
     public static final String DEFAULT_TIMEOUTL = "50000";
     public  String reportFileName;
     private static final String SCREENSHOT_PATH = "screenshots";
     protected  String area;
     private static BufferedWriter loggingWriter;
   //  private ClevertechProperties prop;


     public ClevertechSeleniumUtils(){
        initSelenium();
     }

    public LoggingSelenium getSelenium(){
           return selenium;
     }

    public void setUp(){
        
    }

     public void  initSelenium(){
           if (selenium != null)
               return;
   //      prop = ClevertechProperties.getInstance();
    //     String resultPath = prop.getProperty(ClevertechProperties.REPORT_PATH);
           String resultPath ="Reports";
           String reportDirPath = resultPath + File.separator ;
     //    String screenshotsResultsPath = new File(reportDirPath + File.separator + SCREENSHOT_PATH).getAbsolutePath();

    //     if (!new File(screenshotsResultsPath).exists()){
     //        new File(screenshotsResultsPath).mkdirs();
      //   }
     //        if (!new File(screenshotsResultsPath).exists()){
      //       new File(screenshotsResultsPath).mkdirs();
      //   }
         //final String resultHtmlFileName = resultPath + File.separator + File.separator+ LoggingUtils.timeStampForFileName() +".html";
         final String resultHtmlFileName = resultPath  + File.separator + "Report.html";
         final String resultEncoding = "UTF-8";

         loggingWriter = LoggingUtils.createWriter(resultHtmlFileName, resultEncoding, true);

         LoggingResultsFormatter htmlFormatter = new HtmlResultFormatter(loggingWriter, resultEncoding);

         //htmlFormatter.setScreenShotBaseUri(screenshotsResultsPath+ "/");
         //htmlFormatter.setAutomaticScreenshotPath(screenshotsResultsPath);



      //   String url="http://";
         // wrap HttpCommandProcessor from remote-control
         LoggingCommandProcessor myProcessor = new LoggingCommandProcessor(new HttpCommandProcessor("localhost", 4446, "*firefox","http://binf.clevertech.biz"), htmlFormatter);

         selenium  = new LoggingDefaultSelenium(myProcessor);
         selenium.start();
         selenium.windowMaximize();

     }

     /**
      *  Returns <code>TestData</code> specified for this TestCase
      * @return
      */

     public void tearDown() throws Exception
     {
         selenium.stop();
         try {
             if (null != loggingWriter) {
                 loggingWriter.close();
             }
         } catch (IOException e) {
             // do nothing
         }

     }
     public void captureEntirePageScreenshot() throws Exception
     {

     }
   public static  void waitForCustomCondition(final LoggingSelenium selen){
     new LoggingWait(selen) {
                public boolean until() {
                    return selen.isTextPresent("This Text should never be present");
                }
            }.wait("Wait Example - Designed to fail", Long.parseLong(DEFAULT_TIMEOUTL));
        }

}
